/** 
 * 定义该模块的reducer，根据actionTypes更新state
*/
import * as type from './actionTypes'

// 初始化state
const initState = {
    data:null,//详情数据
    dataSource: [],//表格数据
    total: 0,//总条数
    loading: true,//加载动画
};

const getSuggestData = (state, action) => {
    const { payload } = action;
    let _data = payload.result.data;
    let _total = payload.result.total;

    return Object.assign({}, state, { loading: payload.loading, dataSource: _data, total: _total})
}

const getSuggestById = (state, action) => {
    const { payload } = action;
    let _data = payload.result.data;

    return Object.assign({}, state, { loading: payload.loading, data: _data})
}

// 删除数据之后返回新的state
const dele = (state, action) => {
    const { payload } = action;

    const _data = state.dataSource.filter((item) => {
        return item.id != payload.id
    })

    return Object.assign({}, state, {loading: payload.loading, dataSource: _data})
}
// 根据type执行对应的action操作
const reducerFun = (state = initState, action) => {
    switch (action.type) {
        case type.GET_SUGGEST_DATA_SUCCESS:
            return getSuggestData(state, action)
        case type.DELETE_SUGGEST_BY_ID_SUCCESS:
            return dele(state, action)
        case type.GET_SUGGSET_DATA_BY_ID_SUCCESS:
            return getSuggestById(state, action)
        case type.UPDATE_SUGGSET_DATA_BY_ID_SUCCESS:
            return state
        case type.ADD_SUGGSET_DATA_BY_ID_SUCCESS:
            return state
        default:
            return state
    }
}

export default reducerFun;